#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"

/********************************************************************************************
   
	 
	 
********************************************************************************************/
extern u8  USB_Rx_Demura_End_Flag; //Demura数据全部完成标志
 u32 DemuraRecivedNum=0;      //统计已经接收的数据数量
 u32 DemuraRecivedNum1111=0; 
 u32 DemuraTransOnce;        //每次传输的像素数量
 u8  test_DemuraTransOnce;

extern u8  DemuraIctype;  		  //识别IC型号
extern u8  Demura_OLEDModule_flag;//同型DDIC下的衍生项目
extern u8  Demura_send_flag;
extern u8  DemuraSize_H8;
extern u8  DemuraSize_L8;

extern u32 Demura_START_FLAG;  //Demura 数据传输开始标志

extern u32  USB_DemuraParament_Num;
extern u32 Demura_START_FLAG;  //Demura 数据传输开始标志
extern u32 Demura_Pre_FLAG;    //谱瑞demura配置文件开始标志

void Demura2RAM(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buf2PC[10];
	/*DemuraIctype->IC型号; Demura_OLEDModule_flag->项目号*/
	USB_Rx_Demura_End_Flag=0;	

	if(USB_Rx_Demura==0)//第一笔数据
	{												
		if(DemuraIctype==0x5C)//synapic
		{
			DemuraTransOnce=256*3;
		}
		else if(DemuraIctype==0x5D)//novateck
		{
			DemuraTransOnce=180*3;
		}				
		else //if(DemuraIctype==0x5B)   //raydium & viewtrix
		{
			switch(Demura_OLEDModule_flag)
			{
				case 0:
				DemuraTransOnce=OLED.H_pixel*3;
				break;
				
				case 1:
					DemuraTransOnce=180*3;
				break;
				
				default:
					DemuraTransOnce=OLED.H_pixel*3;
					break;
			}
		}
		
		SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (USB_Rx_Demura_Total_Num)); 
		SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((USB_Rx_Demura_Total_Num)>>16)); 											
		if(OLED.H_pixel*3<=4095)//2828 command单笔上限
		{
			SSD2828_W_Reg(OLED.SigMode,channel,0xBE,(u16) (OLED.H_pixel*3)); 									
		}			
		else
		{	//SSD2828_W_Reg(OLED.SigMode,CS_Master,0xBE,(u16) (OLED.H_pixel*3/2));
			SSD2828_W_Reg(OLED.SigMode,channel,0xBE,(u16)(OLED.H_pixel));
		}       																									
		SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
		if(DemuraIctype!=0x5D)//非Novatek_NT37700C
		{
			SSD2828_W_Cmd(OLED.SigMode,channel,0x2C);
		}
		//Demura_OLEDModule_flag=USB_Rx_Buffer[3]; //同型DDIC下的衍生项目
		USB_Rx_Demura=1; //进入Demura数据写入状态														
	}
	for(i=0;i<USB_ReceivedCount;i++)
	{
		buffer[i+VCP_Receive_True_num] = USB_Rx_Buffer[i];  //
	}
	VCP_Receive_True_num+=USB_ReceivedCount;
	DemuraRecivedNum    +=USB_ReceivedCount;				
	//-----------------------------------------------------------------				
	
	if((VCP_Receive_True_num==DemuraTransOnce)||((DemuraRecivedNum==USB_Rx_Demura_Total_Num))) //写入单笔数据,单次写入DemuraTransOnce个字节
	{												
		if(DemuraIctype==0x5C)  //synapic
		{
			switch(Demura_OLEDModule_flag)
			{		
				case 0x00:   /////GM820-R66455  GM860	
				case 0x02:
				{
					R66455_Flash_Write_Demura(pdev,OLED.SigMode,channel,DemuraRecivedNum,buffer,DemuraTransOnce);		
					break;
				}	
				case 0x01:	
				default:
				{		
					R66456_Flash_Write_Demura(pdev,OLED.SigMode,channel,DemuraRecivedNum,buffer,DemuraTransOnce);	
					break;
				}
				case 0x03:		
					R66451_Flash_Write_Demura(pdev,OLED.SigMode,channel,DemuraRecivedNum,buffer,DemuraTransOnce);
					break;/////GM862-R66451						
			}
		}
		else if(DemuraIctype==0x5D) //novateck
		{
			switch(Demura_OLEDModule_flag)
			{
				case 0x00:
				{
					if(Demura_send_flag==0)
					{
						SSD2828_W_Reg(OLED.SigMode,channel,0xBC,180*3+1);  
						SSD2828_W_Reg(OLED.SigMode,channel,0xBD,0x0000); 		
						//SSD2828_W_Reg(OLED.SigMode,CS_Master,0xBE,(u16) (180*3));
						SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
						SSD2828_W_Data(OLED.SigMode,channel,((buffer[0] << 8) + 0x4c));  //模拟4C 
						Demura_send_flag=1;
					}
					else
					{
						SSD2828_W_Reg(OLED.SigMode,channel,0xBC,180*3+1); 												
						SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
						SSD2828_W_Data(OLED.SigMode,channel,((buffer[0] << 8) + 0x5c)); 
					}											
					SPI_CS_Select(channel,0);
					SSD2828_W_RAM_demura_buffer_part(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce/3,1,0);
					SPI_CS_Select(channel,1);
				}
				break;
				case 0x01:
				{
					//SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
					SSD2828_W_Cmd(OLED.SigMode,channel,0x2C);
					SPI_CS_Select(channel,0);
					SSD2828_W_RAM_buffer_8bits_part_NT37701(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce/3,1,0);
					SPI_CS_Select(channel,1);
					
				}
				break;
			}
													
		}
		else  //raydium & viewtrix
		{												
			switch(Demura_OLEDModule_flag)
			{
				case 0x01:
					if(Demura_send_flag==0)
					{
						SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(180*3)+1);  
						SSD2828_W_Reg(OLED.SigMode,channel,0xBD,0x0000); 		
						//SSD2828_W_Reg(OLED.SigMode,CS_Master,0xBE,(u16) (180*3));
						SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
						SSD2828_W_Data(OLED.SigMode,channel,((buffer[0] << 8) + 0x2c));  //模拟2C 
						Demura_send_flag=1;
					}
					else
					{
						SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(180*3)+1); 												
						SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
						SSD2828_W_Data(OLED.SigMode,channel,((buffer[0] << 8) + 0x3c)); 
					}											
					SPI_CS_Select(channel,0);
					SSD2828_W_RAM_demura_buffer_part(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce/3,1,0);
					SPI_CS_Select(channel,1);
					delay_us(100);
				break;
				
				default:
					SPI_CS_Select(channel,0);
					SSD2828_W_RAM_buffer_8bits_part(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce/3,1,0);
					SPI_CS_Select(channel,1);	
						break;
			}	
												
		}							
		VCP_Receive_True_num=0;
		
		if(DemuraRecivedNum==USB_Rx_Demura_Total_Num)//最后一笔，状态重置
		{
			Demura_START_FLAG=0;  //清除Demura状态
			
			DemuraRecivedNum=0; //累计接收统计归零
			USB_Rx_Demura=0;//进入Demura downloading标志
			Demura_send_flag=0; //Novatek_NT37700C 4c/5c
			//SSD2828_W_Reg(OLED.SigMode,CS_Master,0xC0,0x0001);
			STM2PC_RM671xx(pdev,CDC_IN_EP,buf2PC,5+3);           //返回上位机5个byte数据
		}
	}
}

//---------------------------------瑞鼎Demura数据搬入---------
//extern u32 USB_Rx_Demura_Total_Num;


void DemuraData2DDICRam(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
			
	if(USB_Rx_Demura==0)//第一笔数据
	{	
		SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (USB_Rx_Demura_Total_Num)); 
		SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((USB_Rx_Demura_Total_Num)>>16)); 											
		if(OLED.H_pixel*3<=4095)//2828 command单笔上限
		{
			SSD2828_W_Reg(OLED.SigMode,channel,0xBE,(u16) (OLED.H_pixel*3)); 									
		}			
		else
		{						
			SSD2828_W_Reg(OLED.SigMode,channel,0xBE,(u16)(OLED.H_pixel));
		}					
		SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);					
		{
			SSD2828_W_Cmd(OLED.SigMode,channel,0x2C);
		}
		USB_Rx_Demura=1; //第一次进入Demura数据写入状态														
	}
	//----------------------------------	
	for(i=0;i<USB_ReceivedCount;i++)
	{
		buffer[i+VCP_Receive_True_num] = USB_Rx_Buffer[i];  //
	}
	VCP_Receive_True_num+=USB_ReceivedCount;  //统计是否攒够一行
	DemuraRecivedNum+=USB_ReceivedCount;	  //统计总的接收数量	
	
	if((VCP_Receive_True_num==OLED.H_pixel*3)||(DemuraRecivedNum==USB_Rx_Demura_Total_Num)) //写入单笔数据,单次写入OLED.H_pixel*3字节
	{																					//最后一笔会多送数据，但是多送的数据不会被IC接收	
		{	//写入一行数据											
			SPI_CS_Select(channel,0);
			SSD2828_W_RAM_buffer_8bits_part(OLED.pixel_clk,OLED.SigMode,buffer,OLED.H_pixel,1,0);
			SPI_CS_Select(channel,1);										
		}							
		VCP_Receive_True_num=0;
		
		if(DemuraRecivedNum==USB_Rx_Demura_Total_Num)//最后一笔，状态重置
		{
			DemuraRecivedNum=0; //清空 累计接受数据
			USB_Rx_Demura=0;	  //清空 Demura downloading状态
			Demura_START_FLAG=0;//清空 开始Demura传输的标志位
		}
	}
}

void Demura_shenxian(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buf2PC[10];
	DemuraTransOnce=120;	
	if(USB_Rx_Demura==0)//第一笔数据
	{	
			buffer[1] = 0x2c;
			USB_Rx_Demura=1; //第一次进入Demura数据写入状态
	}
	else buffer[1] = 0x3c;
	buffer[0] = 0x79;         
	for(i=2;i<USB_ReceivedCount+2;i++)
	{
		buffer[i+VCP_Receive_True_num] = USB_Rx_Buffer[i-2];  //
	}
	VCP_Receive_True_num+=USB_ReceivedCount;  //统计是否攒够一行
	DemuraRecivedNum+=USB_ReceivedCount;	  //统计总的接收数量	
	if((VCP_Receive_True_num==DemuraTransOnce)||(DemuraRecivedNum==USB_Rx_Demura_Total_Num)) //写入单笔数据,单次写入OLED.H_pixel*3字节
	{																					//最后一笔会多送数据，但是多送的数据不会被IC接收	

		SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);			
		SSD2828_W_Array(SigMode,channel,buffer,0);
		VCP_Receive_True_num=0;
		
		if(DemuraRecivedNum==USB_Rx_Demura_Total_Num)//最后一笔，状态重置
		{
			DemuraRecivedNum=0; //清空 累计接受数据
			USB_Rx_Demura=0;	  //清空 Demura downloading状态
			Demura_START_FLAG=0;//清空 开始Demura传输的标志位
			STM2PC_RM671xx(pdev,CDC_IN_EP,buf2PC,5+3);           //返回上位机5个byte数据
		}
	}
}

// 谱瑞 demura data写入
void Demura_purui(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{

	if((USB_Rx_Demura==0)&&(VCP_Receive_True_num==0))//第一笔数据
	{	
    buffer[1] = 0x2C;
		test_DemuraTransOnce = 0;
													
	}
	else if((USB_Rx_Demura==1)&&(VCP_Receive_True_num==0))
	{
	   buffer[1] = 0x3C;
	}
	//----------------------------------	
	for(i=0;i<USB_ReceivedCount;i++)
	{
		buffer[2+i+VCP_Receive_True_num] = USB_Rx_Buffer[i];  //
	}
	VCP_Receive_True_num+=USB_ReceivedCount;  //统计是否攒够一行
	DemuraRecivedNum+=USB_ReceivedCount;	  //统计总的接收数量	OLED.H_pixel*3
	
	if((VCP_Receive_True_num==240)||(DemuraRecivedNum==USB_Rx_Demura_Total_Num)) //写入单笔数据,单次写入OLED.H_pixel*3字节
	{																					//最后一笔会多送数据，但是多送的数据不会被IC接收	
    USB_Rx_Demura=1; //第一次进入Demura数据写入状态			
		buffer[0] = VCP_Receive_True_num+1;
		SSD2828_W_Reg(SigMode,channel,0xB7,0x201);			//LP_DCS_Short_Write		
		SSD2828_W_Array(SigMode,channel,buffer,0);
		DemuraRecivedNum1111 = VCP_Receive_True_num;
		VCP_Receive_True_num=0;
		
		if(DemuraRecivedNum==USB_Rx_Demura_Total_Num)//最后一笔，状态重置
		{
			DemuraRecivedNum=0; //清空 累计接受数据
			USB_Rx_Demura=0;	  //清空 Demura downloading状态
			Demura_START_FLAG=0;//清空 开始Demura传输的标志位
			
			
			buffer[0] = 0x2F;
			buffer[1] = 0x03;
			buffer[2] = 0x6d;
			buffer[3] = 0x7e;
			buffer[4] = 0x8a;
			STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,5);   
			
		}
	}
}
//谱瑞芯片写入配置文件
void  DemuraParamentWrite(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
	    u8 buffer1[200];
		  SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);	
			delay_ms(15);
		  buffer1[0]=0x04;
	    buffer1[1]=0xdf;
			buffer1[2]=0xec;
	    buffer1[3]=0x5d;
	    buffer1[4]=0xff;
			SSD2828_W_Array(SigMode,channel,buffer1,0);
	    delay_ms(15);
	    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);	
			delay_ms(15);
		  buffer1[0]=0x04;
	    buffer1[1]=0xdf;
			buffer1[2]=0xec;
	    buffer1[3]=0x6b;
	    buffer1[4]=0x00;
			SSD2828_W_Array(SigMode,channel,buffer1,0);
	    delay_ms(15);
			SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);
      delay_ms(15);		
	    buffer1[1]=0xdf;
			buffer1[2]=0xe0;
	    buffer1[3]=0x35;
	    buffer1[4]=0x10;
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(5);
//	    buffer1[1]=0xdf;
//			buffer1[2]=0xec;
//	    buffer1[3]=0x7e;
//	    buffer1[4]=0x11;
//			SSD2828_W_Array(SigMode,channel,buffer1,0);
//			delay_ms(5);	
	
		  buffer1[0]=0x02;
	    buffer1[1]=0xF1;
			buffer1[2]=0x01;
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(150);
	    buffer1[1]=0xFE;
			buffer1[2]=0x0C;
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(5);
			
			
	    buffer1[0]  = USB_DemuraParament_Num+1;
	    buffer1[1]  = 0x01; 
	    for(int i=2;i<USB_DemuraParament_Num+2;i++)
	    {
		      buffer1[i] = USB_Rx_Buffer[i-2];
		  }
	    SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(1000);

			Demura_Pre_FLAG = 0;   //清除标志

			buffer[0] = 0x2F;
			buffer[1] = 0x07;
			buffer[2] = 0x6d;
			buffer[3] = 0x7e;
			buffer[4] = 0x8a;
			STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,5);        
}



void Demura2RAM_Gen(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buf2PC[10];
	USB_Rx_Demura_End_Flag=0;	

	if(USB_Rx_Demura==0)//第一笔数据
	{												
		
		DemuraTransOnce=((DemuraSize_H8)<<8)+DemuraSize_L8;
		
		
		SSD2828_W_Reg(OLED.SigMode,channel,0xBC,(u16) (USB_Rx_Demura_Total_Num)); 
		SSD2828_W_Reg(OLED.SigMode,channel,0xBD,(u16)((USB_Rx_Demura_Total_Num)>>16)); 											
		if(OLED.H_pixel*3<=4095)//2828 command单笔上限
		{
			SSD2828_W_Reg(OLED.SigMode,channel,0xBE,(u16) (OLED.H_pixel*3)); 									
		}			
		else
		{	
			SSD2828_W_Reg(OLED.SigMode,channel,0xBE,(u16)(OLED.H_pixel));
		}       																									
		SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
		if(DemuraIctype!=0x10)//非Novatek_NT37700C
		{
			SSD2828_W_Cmd(OLED.SigMode,channel,0x2C);
		}
		USB_Rx_Demura=1; //进入Demura数据写入状态														
	}
	for(i=0;i<USB_ReceivedCount;i++)
	{
		buffer[i+VCP_Receive_True_num] = USB_Rx_Buffer[i];  //
	}
	VCP_Receive_True_num+=USB_ReceivedCount;
	DemuraRecivedNum    +=USB_ReceivedCount;				
	//-----------------------------------------------------------------				
	
	if((VCP_Receive_True_num==DemuraTransOnce)||((DemuraRecivedNum==USB_Rx_Demura_Total_Num))) //写入单笔数据,单次写入DemuraTransOnce个字节
	{
		if(DemuraIctype==0x10)
		{
			if(Demura_send_flag==0)
			{
				SSD2828_W_Reg(OLED.SigMode,channel,0xBC,DemuraTransOnce+1);  
				SSD2828_W_Reg(OLED.SigMode,channel,0xBD,0x0000); 		
				SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
				SSD2828_W_Data(OLED.SigMode,channel,((buffer[0] << 8) + 0x4c));  //模拟4C 
				Demura_send_flag=1;
			}
			else
			{
				SSD2828_W_Reg(OLED.SigMode,channel,0xBC,DemuraTransOnce+1); 	//模拟5C											
				SSD2828_W_Cmd(OLED.SigMode,channel,0xBF);
				SSD2828_W_Data(OLED.SigMode,channel,((buffer[0] << 8) + 0x5c)); 
			}											
			SPI_CS_Select(channel,0);
			SSD2828_W_RAM_demura_buffer_part(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce/3,1,0);
			SPI_CS_Select(channel,1);
		}
		else
		{
				SPI_CS_Select(channel,0);
				SSD2828_W_RAM_buffer_8bits_part_NT37701(OLED.pixel_clk,OLED.SigMode,buffer,DemuraTransOnce/3,1,0);
				SPI_CS_Select(channel,1);
		}												
					
		VCP_Receive_True_num=0;
		
		if(DemuraRecivedNum==USB_Rx_Demura_Total_Num)//最后一笔，状态重置
		{
			Demura_START_FLAG=0;  //清除Demura状态			
			DemuraRecivedNum=0; //累计接收统计归零
			USB_Rx_Demura=0;//进入Demura downloading标志
			Demura_send_flag=0; //Novatek_NT37700C 4c/5c
			STM2PC_RM671xx(pdev,CDC_IN_EP,buf2PC,5+3);           //返回上位机5个byte数据
		}
	}
}